package com.woniuxy.tfss.utils;

import javax.servlet.http.HttpServletRequest;

import com.woniuxy.tfss.entity.Category;
import com.woniuxy.tfss.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Random;

/**
 * 工具方法
 */
public class MyUtils {
    public static String getNow() {//获取当前时间的方法
        Date t = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(t);
    }

    /**
     * 得到请求中的IP
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static String getOrderNo() {//根据时间戳生成订单编号
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return "TFSS" + fmt.format(now) + (int) (Math.random() * 90 + 10);
    }

    public static String getRechargeNo() {//根据时间戳生成订单编号
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return "RECH" + fmt.format(now) + (int) (Math.random() * 90 + 10);
    }

    public static BigDecimal itemPrice(int distance, int categoryId, CategoryService categoryService) {
        //清单小计的计算方法,参数1：距离(单位：m),参数2：物品种类id
        //5km米以内为起步价,之后每km按照物品单价计算,不足1km按1km算
        Category category = categoryService.getById(categoryId);
        if (distance <= 5000) {
            return category.getStartPrice();//5km以内
        } else {
            if ((distance - 5000) % 1000 == 0) {
                //整数公里
                return category.getStartPrice().add(category.getPrice().multiply(new BigDecimal((distance - 5000) / 1000))).setScale(2);
            } else {
                //有补足情况
                return category.getStartPrice().add(category.getPrice().multiply(new BigDecimal((distance - 5000) / 1000 + 1))).setScale(2);
            }
        }
    }

    /**
     * 产生4位随机数(0000-9999)
     *
     * @return 4位随机数
     */
    public static String getFourRandom() {
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";//生成0-10000的随机数
        int randLength = fourRandom.length();//随机数小于1000，长度小于4位
        if (randLength < 4) { //不够四位在最高位补0
            for (int i = 1; i <= 4 - randLength; i++)
                fourRandom = "0" + fourRandom;
        }

        return fourRandom;
    }

}
